//
//  Extension_LoginViewController.swift
//  MovieApp
//
//  Created by SuPeiSen on 2020/11/26.
//

import Foundation
import UIKit
import SnapKit
import AutoInch
import Hue
import RxSwift
import RxCocoa


 // MARK: setUI
extension LoginViewController {    
    public func setUI() {
        // 添加一个滚动容器在最外层
        let mainView = UIScrollView()
        self.view.addSubview(mainView)
        mainView.snp.makeConstraints { (make) in
            make.width.equalTo(self.view.snp.width)
            make.height.equalTo(self.view.snp.height)
        }
        // 背景图片(获取随机壁纸：http://lorempixel.com/1080/1920/)
        let loginBgImageView = UIImageView(image: UIImage(named: "login_bg"))
        loginBgImageView.contentMode = .scaleAspectFill
        loginBgImageView.setFuzz(value: 20)
        mainView.addSubview(loginBgImageView)
        loginBgImageView.snp.makeConstraints { (make) in
            make.width.equalToSuperview()
            make.height.equalToSuperview()
        }
        
        // 登陆输入
        let loginInputView = UIView()
        mainView.addSubview(loginInputView)
        loginInputView.snp.makeConstraints { (make) in
            make.width.equalTo(240.auto())
            make.height.equalTo(320.auto())
            make.centerX.equalToSuperview()
            make.top.equalTo(mainView.snp.top).offset(150.auto())
        }
        loginInputView.backgroundColor = UIColor.white
        loginInputView.isHidden = true
        loginInputView.layer.cornerRadius = 10.auto()
        
        // 左上角light
        let lightImgaeView = UIImageView(image: UIImage(named: "light"))
        loginInputView.addSubview(lightImgaeView)
        lightImgaeView.snp.makeConstraints { (make) in
            make.top.equalTo(loginInputView.snp.top).offset(10.auto())
            make.width.height.equalTo(80.auto())
            make.centerX.equalToSuperview()
        }
        
        //close
        let closeButton = UIButton(type: .custom)
        loginInputView.addSubview(closeButton)
        closeButton.setImage(UIImage(named: "close"), for: .normal)
        closeButton.snp.makeConstraints { (make) in
            make.right.equalTo(loginInputView.snp.right)
            make.top.equalTo(loginInputView.snp.top)
            make.width.height.equalTo(20.auto())
        }
        closeButton.rx.tap.subscribe(onNext: { [weak self] in
            if(!loginInputView.isHidden) {
                UIView.animate(withDuration: 1) {
                    loginInputView.alpha = 0
                } completion: { (bool) in
                    if(bool) {
                        loginInputView.alpha = 1
                        loginInputView.isHidden = true
                        loginInputView.backgroundColor = UIColor.white
                    }
                }
                self?.buttonOffSet.update(offset: 50.auto())
                UIView.animate(withDuration: 1) {
                    self?.view.layoutIfNeeded()
                }
            }
        }).disposed(by: self.disposeBag)
        
        //MARK: 用户名输入框
        let userNameInput = UITextField()
        userNameInput.borderStyle = .bezel
        loginInputView.addSubview(userNameInput)
        userNameInput.snp.makeConstraints { (make) in
            make.width.equalToSuperview().multipliedBy(0.8)
            make.height.equalTo(50.auto())
            make.top.equalTo(lightImgaeView.snp.bottom).offset(30.auto())
            make.centerX.equalToSuperview()
        }
        userNameInput.layer.borderWidth = 0.5
        userNameInput.layer.borderColor = UIColor.black.cgColor
        userNameInput.placeholder = "请输入用户名"
        userNameInput.clearButtonMode = .whileEditing
        
        //MARK: 密码输入框
        let passwordInput = UITextField()
        passwordInput.borderStyle = .bezel
        loginInputView.addSubview(passwordInput)
        passwordInput.snp.makeConstraints { (make) in
            make.width.equalToSuperview().multipliedBy(0.8)
            make.height.equalTo(50.auto())
            make.top.equalTo(userNameInput.snp.bottom).offset(20.auto())
            make.centerX.equalToSuperview()
        }
        passwordInput.layer.borderWidth = 0.5
        passwordInput.layer.borderColor = UIColor.black.cgColor
        passwordInput.placeholder = "请输入密码"
        passwordInput.clearButtonMode = .whileEditing
        
        // MARK: 登陆按钮
        let loginButton = UIButton()
        mainView.addSubview(loginButton)
        loginButton.snp.makeConstraints { (make) in
            make.width.equalTo(180.auto())
            make.height.equalTo(40.auto())
            make.centerX.equalToSuperview()
            self.buttonOffSet = make.top.equalTo(loginInputView.snp.bottom).offset(50).constraint
        }
        loginButton.backgroundColor = .black
        loginButton.setTitle("登陆", for: .normal)
        loginButton.setTitleColor(.white, for: .normal)
        loginButton.setTitleColor(UIColor.white.alpha(0.5), for: .highlighted)
        loginButton.layer.cornerRadius = 20.auto()
        loginButton.rx.tap.subscribe(onNext: { [weak self] in
            if(loginInputView.isHidden) {
                self?.view.setNeedsLayout()
                UIView.transition(with: loginInputView, duration: 1, options: .transitionCurlDown) {
                    loginInputView.isHidden = false
                }
                self?.buttonOffSet.update(offset: -20.auto())
                UIView.animate(withDuration: 1) {
                    self?.view.layoutIfNeeded()
                }
            }else {
                appPrint()
            }
        }).disposed(by: self.disposeBag)
        
        // MARK: 提示
        let checkTip = UILabel()
        loginInputView.addSubview(checkTip)
        checkTip.text = "用户名和密码长度必须大于8"
        checkTip.snp.makeConstraints { (make) in
            make.top.equalTo(passwordInput.snp.bottom).offset(15)
            make.left.greaterThanOrEqualTo(passwordInput)
        }
        checkTip.textColor = .red
        checkTip.font = .systemFont(ofSize: 12)
        
//        loginViewModel = LoginViewModel(userName: userNameInput.rx.text.orEmpty.asObservable(), password: passwordInput.rx.text.orEmpty.asObservable(), inputViewIsHidden: inputViewIsHidden)
//        
//        loginViewModel.everythingValid
//            .bind(to: loginButton.rx.isEnabled)
//            .disposed(by: self.disposeBag)
//        
//        
//        inputViewIsHidden.subscribe { (bool) in
//            appPrint(bool, msg: "inputViewIsHidden.subscribe")
//        }.disposed(by: self.disposeBag)
        
        // 登陆按钮
        let loginViewModel = LoginViewModel(userName: userNameInput.rx.text.orEmpty.asObservable(), password: passwordInput.rx.text.orEmpty.asObservable())
        
        loginViewModel.everythingValid
            .bind(to: checkTip.rx.isHidden)
            .disposed(by: self.disposeBag)

//        loginViewModel.everythingValid
//            .subscribe {
//                appPrint($0, msg: "loginViewModel.everythingValid")
//            }
//            .disposed(by: self.disposeBag)
        
        loginViewModel.userNameValid
            .subscribe{name in
                appPrint(name.element, msg: "name")
            }
            .disposed(by: self.disposeBag)
        
        
        
        
        
//        loginViewModel.everythingValid
//            .bind(to: loginButton.rx.isEnabled)
//            .disposed(by: self.disposeBag)
//        
//        loginViewModel.buttonIsEnabled.onNext(true)
//        loginViewModel.buttonIsEnabled
//            .bind(to: loginButton.rx.isEnabled)
//            .disposed(by: self.disposeBag)
        
//        loginViewModel.showLoginView
//            .bind(to: loginInputView.rx.isHidden)
//            .disposed(by: self.disposeBag)
        
    }
}
